package com.xml;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProtoCreate
{
    private final static String filePath = "E:\\proto";
    static String java_package_service = "proto.cn.migu.deal.bookseller";

    /**
     * 根据元素创建proto文件
     * @param list ele集合
     * @param filePre 文件名前缀
     * @param fileSuf 文件名后缀
     */
    public static void createProtoWithEle(List<Element> list, String filePre, String fileSuf){
        Map<String,String> map = new HashMap<>();

        for (Element ele : list)
        {
            String typeName = "";
            Attribute type = ele.attribute("type");
            if(null!=type){
                typeName = type.getValue();
            }
            map.put(ele.getName(),typeName);
        }

        File file = new File(filePath+"/"+filePre+fileSuf+".proto");
        String newName = filePre + fileSuf;
        writeContent(file,map,fileSuf,newName);
    }


    /**
     *
     * @param file
     * @param fileSuf
     */
    public static void writeContent(File file,Map<String,String> map,String fileSuf,String newName){
        try
        {
            CharArrayWriter tempStream = new CharArrayWriter();
            // 替换
            String line = null;
            setImport(tempStream,fileSuf);
            createClass(tempStream,newName);
            int i = 0;
            for (String key : map.keySet())
            {
                i++;
                line = createMethod(line,key,map.get(key),i);
                if (null != line)
                {
                    tempStream.write(line);
                    tempStream.append(System.getProperty("line.separator"));
                }
            }

            tempStream.write("}");
            FileWriter out = new FileWriter(file);
            tempStream.writeTo(out);
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void setImport(CharArrayWriter tempStream,String fileSuf)
    {
        String profix =
                "syntax = \"proto3\";\n" +
                        "option java_package = \"com.migu.reading.ctu.api."+fileSuf.toLowerCase()+"\";\n" +
                        "option java_multiple_files = true;\n";
        try
        {
            tempStream.write(profix);
            tempStream.append(System.getProperty("line.separator"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void setImportService(CharArrayWriter tempStream,String fileName)
    {
        String profix =
                "syntax = \"proto3\";\n" +
                        "option java_package= \""+ java_package_service+" \";\n" +
                        "option java_outer_classname = \""+"_"+fileName+"\";\n" +
                        "option java_generic_services = true;\n" +
                        "option java_multiple_files = true;";
        try
        {
            tempStream.write(profix);
            tempStream.append(System.getProperty("line.separator"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static String createMethod(String line,String key,String value,int i)
    {
        line = value +" "+ key + "= "+i + ";";
        return line;
    }

    private static void createClass(CharArrayWriter tempStream,String fileName)
    {

        String line = "message  " +fileName+" {";
        try
        {
            tempStream.write(line);
            tempStream.append(System.getProperty("line.separator"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
